<template>
  <div class="group-buy-view">
    <div class="price-wrap">
      <div class="left">
        <div class="on-sale-price">
          <div class="unit">￥</div>
          <div class="onSalePrice">{{ options.pay_money || 9.9 }}</div>
        </div>
        <div class="left-right">
          <div class="remaining-quota">
            单独购买￥{{ options.original_cost || 9.9 }}
          </div>
          <!-- <div class="originPrice">
									团购{{ options.pay_money || 9.9 }}元抵扣{{
										(options.original_cost || 0) - (options.pay_money || 0)
									}}元
								</div> -->
        </div>
      </div>
      <div
        class="right price-number"
        v-if="!options.discount_money || options.discount_money * 1 == 0"
      >
        {{ (options.original_cost || 0) - (options.pay_money || 0) }}
      </div>
      <div class="right price-number" v-else>{{ options.discount_money }}</div>
    </div>
    <div class="form-wrap">
      <div class="tips">
        还差<span class="num">1</span>人成团，剩余
        <van-count-down
          ref="pageCountDown"
          :time="countDownTime"
          format="mm 分 ss 秒"
        >
          <template #default="timeData">
            <span class="block">0{{ timeData.minutes }}</span>
            <span class="colon">分</span>
            <span class="block">{{
              timeData.seconds > 9 ? timeData.seconds : "0" + timeData.seconds
            }}</span>
            <span class="colon">秒</span>
          </template>
        </van-count-down>
      </div>
      <div class="marquee">
        <van-swipe
          style="height: 1rem"
          :autoplay="4500"
          :show-indicators="false"
          vertical
          loop
          :touchable="false"
        >
          <van-swipe-item v-for="(v, i) in marqueeUsers" :key="i">
            <div class="marquee-item">
              <img :src="require(`@/assets/image/user/${v[0]}`)" />
              <img :src="require(`@/assets/image/user/${v[1]}`)" />
              <img src="@/assets/image/12.png" @click="openChoiceCity" />
            </div>
          </van-swipe-item>
        </van-swipe>
      </div>
      <div class="section-title">
        <div class="separator" />
        <div class="title">选择城市参团</div>
        <div class="separator" />
      </div>
      <city-list :list="options.city_data" @select="choiceCityConfirm" />
    </div>
    <group-buy-fixed
      :view="view"
      :fixed-views="fixedViews"
      @click="openChoiceCity"
      @reset="resetCountDown"
    />
    <choice-city
      title="选择城市参团"
      :list="options.city_data"
      ref="choiceCity"
      @confirm="choiceCityConfirm"
    />
    <fill-in-form ref="form" @submit="submit" />
  </div>
</template>
<script>
import { randomNum } from "@/utils";
import CityList from "../city-list";
import ChoiceCity from "../choice-city";
import formMixin from "../../../mixins";
import FillInForm from "../form/group-buy3.vue";
import GroupBuyFixed from "../group-buy-fixed";

export default {
  name: "group-buy-view",
  mixins: [formMixin],
  components: {
    CityList,
    ChoiceCity,
    FillInForm,
    GroupBuyFixed,
  },
  props: {
    view: {
      type: Object,
      default: () => ({}),
    },
    fixedViews: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      users: [],
      marqueeUsers: [],
      countDownTime: 10 * 60 * 1000,
      activeCity: "",
    };
  },
  computed: {
    options() {
      return this.view.options || {};
    },
    directPay() {
      return this.options.form_pay == 2;
    },
  },
  created() {
    this.loopMarqueeUsers();
  },
  methods: {
    loopMarqueeUsers() {
      let list = [];
      for (let i = 0; i < 15; i++) {
        list.push([randomNum(60) + ".jpeg", randomNum(60) + ".jpeg"]);
      }
      this.marqueeUsers = list;
      let timer = setTimeout(() => {
        this.loopMarqueeUsers();
      }, 14 * 5 * 1000);
      this.$once("hook:beforeDestroy", () => {
        clearTimeout(timer);
      });
    },
    resetCountDown() {
      this.$refs.pageCountDown?.reset?.();
    },
    openChoiceCity() {
      this.activeCity = "";
      if (this.directPay) {
        this.submit();
        return;
      }
      this.$refs.choiceCity.open();
    },
    choiceCityConfirm(v) {
      this.activeCity = v;
      if (this.directPay) {
        this.submit();
        return;
      }
      this.$refs.form.open();
    },
    async submit(data = {}) {
      const { query } = this.$route;
      const { biz_id, id: view_id } = this.tpl;
      const { is_payment, form_pay, pay_money } = this.options;
      let form = {
        tg_id: query.id,
        tg_type: query.type,
        mini_id: query.mini_id || '',
        order_sn: this.order_sn,
        biz_id,
        view_id,
        is_payment,
        form_type: 1,
        form_pay,
        pay_money,
        mould_type: this.view.type,
        ...data,
        // user_id
      };

      let data_set = {
        ...(data.data_set || {}),
        city: this.activeCity,
      };
      form.data_set = JSON.stringify(data_set);
      this.$emit("submit");
      await this.adSubmit(form, "ads3", this.view);
      this.$refs.form.close();
    },
  },
};
</script>
<style lang="scss" scoped>
.group-buy-view {
  .price-wrap {
    display: flex;
    background: no-repeat url(~@/assets/image/10.jpg);
    background-size: cover;
    color: #fff;
    height: 1.5866rem;
    position: relative;
    .left {
      display: flex;
      align-items: center;
      flex: 1;
      .on-sale-price {
        display: flex;
        align-items: baseline;
        margin-left: 0.3111111rem;
        font-weight: bold;
        color: #ffffff;
        margin-right: 5px;
        .unit {
          font-size: 0.355555rem;
        }
        .onSalePrice {
          line-height: 1rem;
          font-size: 0.844444rem;
          margin-top: 1px;
        }
      }
      .left-right {
        margin-left: 4px;
        margin-top: 4px;
        .remaining-quota {
          display: inline-block;
          height: 0.355555rem;
          line-height: 0.355555rem;
          padding: 0 0.2rem 0 0.2222222rem;
          font-size: 0.24444444rem;
          text-align: center;
          color: #ff041e;
          background: linear-gradient(180deg, #ffffff -6.05%, #ffd879 100%);
          box-shadow: 0px 2px 4px #f00035;
          border-radius: 0.177777rem 0.177777rem 0.177777rem 0;
        }
        .originPrice {
          height: 0.44444444rem;
          line-height: 0.44444444rem;
          opacity: 0.8;
          font-size: 0.2222222rem;
          font-weight: 400;
          color: #ffffff;
        }
      }
    }

    .right {
      position: absolute;
      right: 0.33333rem;
      bottom: 0.2444444rem;
      font-weight: 600;
      font-size: 0.33333rem;
      line-height: 15px;
      color: #fff2d2;
      &::before {
        content: "￥";
        font-size: 0.3rem;
        font-weight: 600;
      }
    }
  }
  .form-wrap {
    margin: 0.26666rem;
    padding: 0.48888rem 0.22222rem;
    background: #fff;
    border-radius: 6px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    .tips {
      display: flex;
      align-items: center;
      .num {
        color: #ff4b00;
      }
      .van-count-down {
        margin-left: 4px;
      }
      .colon,
      .block {
        display: inline-block;
      }
      .colon {
        padding: 0px 3px;
        color: #fe473d;
      }
      .block {
        width: 30px;
        color: #fff;
        font-size: 12px;
        padding: 0px 6px;
        border-radius: 3px;
        text-align: center;
        background-color: #fe473d;
      }
    }
    .marquee {
      margin: 0.355555rem auto;
      height: 1rem;
      &-item {
        padding: 0.1rem 0;
        img {
          height: 0.8rem;
          width: 0.8rem;
          border-radius: 50%;
          border: 1px solid #e9e9e9;
          + img {
            margin-left: 4px;
          }
        }
      }
    }
  }
}
.section-title {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  margin-bottom: 0.2rem;
  .title {
    margin: 0 0.1777777rem;
    font-size: 0.44444rem;
    font-weight: bold;
  }
  .separator {
    width: 0.888888rem;
    height: 1px;
    background: #000;
    transform: scaleY(0.5);
  }
}
</style>
